package cn.fanyamin.web.struts;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import org.apache.commons.lang.math.NumberUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.validator.LazyValidatorForm;

import cn.fanyamin.business.ToolService;
import cn.fanyamin.domain.UserCategory;
import cn.fanyamin.domain.Site;
import cn.fanyamin.domain.User;

public class SiteAction extends SecureBaseAction {
    private static Log logger = LogFactory.getLog(AddressAction.class);

    public ActionForward list(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
           
        
        User user = getUserFromSession(request);
        
        setCategories(request,UserCategory.CATEGORY_TYPE_BOOK);

        List<Site> siteList = toolService.listSite(user.getUserID());
        request.setAttribute("siteList", siteList);
        logger.info("siteList: " + siteList);
        return new ActionForward("/WEB-INF/jsp/struts/tool/sites.jsp");
    }
    
    public ActionForward create(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        Site site = getSiteFromForm(form);
        if(site == null) {
            return mapping.findForward("failure");
        }
        User user = getUserFromSession(request);
        site.setUserID(user.getUserID());
        int siteID = toolService.createSite(site);
        site.setSiteID(siteID);
        logger.info("create site: " + site);
        //return mapping.findForward("success");
        return new ActionForward("/WEB-INF/jsp/struts/tool/sites.jsp");
    }
    
    private Site getSiteFromForm(ActionForm form) {
        Site site = new Site();
        LazyValidatorForm siteForm = (LazyValidatorForm) form;

        site.setSiteID(NumberUtils.toInt((String) siteForm.get("siteID")));
        site.setSiteName((String) siteForm.get("siteName"));  
        site.setSiteUrl((String) siteForm.get("siteUrl"));    


        return site;
    }

    public ActionForward retrieve(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        LazyValidatorForm siteFrom = (LazyValidatorForm)form;
        int siteID = NumberUtils.toInt((String)siteFrom.get("siteID"));
        Site site = toolService.retrieveSite(siteID);
        request.setAttribute("site", site);
        return new ActionForward("/WEB-INF/jsp/struts/tool/sites.jsp");
    }
    
        
    public ActionForward update(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        
        ToolService toolService = getWebApp().getToolService();
        Site site = getSiteFromForm(form);
        toolService.updateSite(site);
        //request.setAttribute("siteType", site.getSiteType());
        logger.info("update site, site=" + site);
        return new ActionForward("/WEB-INF/jsp/struts/tool/sites.jsp");
    }
    
    public ActionForward delete(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        LazyValidatorForm siteFrom = (LazyValidatorForm)form;
        int siteID = NumberUtils.toInt((String)siteFrom.get("siteID"));
        toolService.deleteSite(siteID);
        logger.info("delete site, siteID=" + siteID);
        //return mapping.findForward("success");
        return new ActionForward("/WEB-INF/jsp/struts/tool/sites.jsp");//redirect
    }
    
    public ActionForward add(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        logger.info("add site");
        
        setCategories(request,UserCategory.CATEGORY_TYPE_BOOK);
        
        return new ActionForward("/WEB-INF/jsp/struts/tool/site.jsp");
    }
    
    public ActionForward edit(ActionMapping mapping, ActionForm form,
                                HttpServletRequest request, HttpServletResponse response) {
        ToolService toolService = getWebApp().getToolService();
        LazyValidatorForm siteFrom = (LazyValidatorForm)form;
        int siteID = NumberUtils.toInt((String)siteFrom.get("siteID"));
        Site site = toolService.retrieveSite(siteID);
        request.setAttribute("site", site);
        
        setCategories(request,UserCategory.CATEGORY_TYPE_BOOK);
        
        return new ActionForward("/WEB-INF/jsp/struts/tool/site.jsp");
    }
}
